from datetime import timedelta

from flask import Flask, render_template, request,session
from flask_cors import CORS

from data import data
app = Flask(__name__)
CORS(app)
CORS(app, resources={r"/*": {"origins": "*","supports_credentials": True}})
app.config['SESSION_COOKIE_SAMESITE'] = 'None'
app.config['SESSION_COOKIE_SECURE'] = True
app.config['SESSION_COOKIE_DOMAIN'] = None  # 或  用于开发
app.secret_key="123456"



@app.route('/')
def index():
    return render_template("index.html")

@app.route('/stu')
def stu():
    date = data.querystu()
    return render_template("stu.html", date=date)
@app.route('/addstu')
def addstu():
    xi = data.queryxi()
    major = data.querymajor()
    return render_template('addstu.html', xi=xi,major=major)
@app.route('/addstucon')
def addstucon():
    name = request.args.get('name')
    sex = request.args.get('sex')
    age = request.args.get('age')
    xi = request.args.get('xi')
    major = request.args.get('major')
    data.addstu(name,sex,age,xi,major)
    return render_template('ok.html',url='/stu',message='添加')
@app.route('/removestu')
def removestu():
    id = request.args.get('id')
    data.deletestu(id)
    return render_template('ok.html',url='/stu',message='删除')
@app.route('/updatestu')
def updatestu():
    stu = eval(request.args.get('date'))
    xi = data.queryxi()
    major = data.querymajor()
    return render_template('updatestu.html',stu=stu,xi=xi,major=major)
@app.route('/updatestucon')
def updatestucon():
    id = request.args.get('id')
    name = request.args.get('name')
    sex = request.args.get('sex')
    age = request.args.get('age')
    xi = request.args.get('xi')
    major = request.args.get('major')
    data.updatestu(id,name,sex,age,xi,major)
    return render_template('ok.html',url='/stu',message='修改')




@app.route('/xi')
def xi():
    date = data.queryxi()
    return render_template("xi.html", date=date)
@app.route('/addxi')
def addxi():
    return render_template('addxi.html')
@app.route('/addxicon')
def addxicon():
    xi = request.args.get('xi')
    data.addxi(xi)
    return render_template('ok.html',url='/xi',message='添加')
@app.route('/removexi')
def removexi():
    id = request.args.get('id')
    data.deletexi(id)
    return render_template('ok.html',url='/xi',message='删除')
@app.route('/updatexi')
def updatexi():
    date = eval(request.args.get('date'))
    return render_template('updatexi.html',date=date)
@app.route('/updatexicon')
def updatexicon():
    id = request.args.get('id')
    xi = request.args.get('xi')
    data.updatexi(id,xi)
    return render_template('ok.html',url='/xi',message='修改')



@app.route('/major')
def major():
    date = data.querymajor()
    return render_template("major.html", date=date)
@app.route('/addmajor')
def addmajor():
    return render_template('addmajor.html')
@app.route('/addmajorcon')
def addmajorcon():
    xi = request.args.get('xi')
    major = request.args.get('major')
    data.addmajor(xi,major)
    return render_template('ok.html',url='/major',message='添加')
@app.route('/removemajor')
def removemajor():
    id = request.args.get('id')
    data.deletemajor(id)
    return render_template('ok.html',url='/major',message='删除')
@app.route('/updatemajor')
def updatemajor():
    date = eval(request.args.get('date'))
    return render_template('updatemajor.html',date=date)
@app.route('/updatemajorcon')
def updatemajorcon():
    id = request.args.get('id')
    xi = request.args.get('xi')
    major = request.args.get('major')
    data.updatemajor(id,xi,major)
    return render_template('ok.html',url='/major',message='修改')

@app.before_request
def before_request():
    if request.path != '/login' and request.path != '/register':
        if session.get('login') != "true":
            return "fail"
@app.route('/login',methods=['GET','POST'])
def login():
    user = request.get_json().get('username')
    psw = request.get_json().get('password')
    sql = 'select count(*) from sys_user where username =%s AND `password`=%s'
    result = data.mysql.select_one(sql,(user,psw))
    if result['count(*)'] > 0:
        session['login'] = "true"
        return {"state":"ok","username":user,"psw":psw}
    else:
        return {"state":"fail","username":"","psw":""}

@app.route('/register', methods=['GET','POST'])
def register():
    user = request.get_json().get('username')
    psw = request.get_json().get('password')
    sql = 'insert into sys_user (username,password) values  (%s,%s)'
    res = data.mysql.insert_one(sql, (user, psw))
    if res > 0:
        return "ok"
@app.route('/firstli')
def firstli():
    print(session.get('Pycharm-22adcde7'))
    sql = 'select * from fist_li'
    result = data.mysql.select_all(sql)
    return result
@app.route('/addfirstli')
def addfirstli():
    title = request.args.get('title')
    sql = "insert into fist_li(title) values(%s)"
    data.mysql.insert_one(sql, (title))
    return "ok"

@app.route('/updatefirstli')
def updatefirstli():
    id = request.args.get('id')
    title = request.args.get('title')
    sql = "update fist_li set title=%s where id=%s"
    data.mysql.update_one(sql, (id,title))
    return "ok"
@app.route('/deletefirstli')
def deletefirstli():
    id = request.args.get('id')
    sql = "delete t1, t2 from fist_li t1 left join second_li t2 on t1.id = t2.pid where t1.id = %s"
    data.mysql.delete_one(sql, (id))
    return "ok"
@app.route('/secondli')
def secondli():
        pid = request.args.get('pid')
        sql = 'select * from second_li where pid=%s'
        result = data.mysql.select_many(sql,(pid))
        if type(result)==tuple:
            return []
        else:
            return result


@app.route('/addsecondli')
def addsecondli():
    con = request.args.get('con')
    state = request.args.get('state')
    date = request.args.get('date')
    pid = request.args.get('pid')
    sql = "insert into second_li(con,state,date,pid) values(%s,%s,%s,%s)"
    data.mysql.insert_one(sql, (con,state,date,pid))
    return "ok"

@app.route('/updatesecondli')
def updatesecondli():
    id = request.args.get('id')
    con = request.args.get('con')
    state = request.args.get('state')
    date = request.args.get('date')
    sql = "update second_li set con=%s,state=%s,date=%s where id=%s"
    data.mysql.update_one(sql, (con,state,date,id))
    return "ok"
if __name__ == '__main__':
    app.run(debug=True)


